<template>
  <el-form label-width="100px">
    <el-form-item label="Sku名称">
      <el-input
        placeholder="SKU名称"
        v-model="skuParams.skuName"
        clearable
        @change=""
      ></el-input>
    </el-form-item>
    <el-form-item label="Sku价格(元)">
      <el-input
        placeholder="Sku价格(元)"
        type="number"
        v-model="skuParams.price"
        clearable
        @change=""
      ></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input
        placeholder="重量(g)"
        v-model="skuParams.weight"
        type="number"
        clearable
        @change=""
      ></el-input>
    </el-form-item>
    <el-form-item label="Sku描述">
      <el-input
        placeholder="Sku描述"
        type="textarea"
        v-model="skuParams.skuDesc"
        clearable
        @change=""
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true">
        <el-form-item
          :label="item.attrName"
          v-for="item in attrArr"
          :key="item.id"
        >
          <el-select v-model="item.attrIdAndValueId" style="width: 100px">
            <el-option
              :value="`${item.id}:${attrValue.valueName}`"
              :label="attrValue.valueName"
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item
          :label="item.saleAttrName"
          v-for="item in saleArr"
          :key="item.id"
        >
          <el-select v-model="item.saleIdAndValueId" style="width: 100px">
            <el-option
              :value="`${item.id}:${saleAttrValue.id}`"
              :label="saleAttrValue.saleAttrValueName"
              v-for="saleAttrValue in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column
          type="selection"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="图片">
          <template v-slot="{ row }">
            <img :src="row.imgUrl" alt="" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="small" @click="handler(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="primary" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import type { SkuData } from '@/apis/product/spu/type'
import { reactive, ref } from 'vue'
//引入请求的Api
import { reqAttr } from '@/apis/product/attr/index'
import {
  reqSpuImageList,
  reqSpuHasSaleAttr,
  reqAddSku,
} from '@/apis/product/spu/index'
import { ElMessage } from 'element-plus'
//自定义事件
let $emit = defineEmits(['changeScene'])
//存储平台属性
let attrArr = ref<any>([])
//销售属性
let saleArr = ref<any>([])
//照片的数据
let imgArr = ref<any>([])
//收集SKU的参数
let skuParams = reactive<SkuData>({
  //父组件传递过来的数据
  category3Id: '',
  spuId: '',
  tmId: '',
  //v-model收集
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  // skuSaleAttrValueList:[
  // ],
  skuDefaultImg: 'string',
})
//获取table组件实例
let table = ref<any>()

//当前子组件的方法对外暴露
//获取平台属性
const initSkuData = async (
  c1Id: number | string,
  c2Id: number | string,
  c3Id: number | string,
  spu: any,
) => {
  //收集数据
  skuParams.category3Id = c3Id
  skuParams.spuId = spu.id
  skuParams.tmId = spu.tmId

  //获取平台属性
  let result: any = await reqAttr(c1Id, c2Id, c3Id)
  //获取对应的销售属性
  let result1: any = await reqSpuHasSaleAttr(spu.id)
  //获取照片墙的数据
  let result2: any = await reqSpuImageList(spu.id)
  //平台属性
  attrArr.value = result.data
  //销售属性
  console.log(result1)

  saleArr.value = result1.data
  //图片
  imgArr.value = result2.data
}
//设置默认图片方法的回调
const handler = (row: any) => {
  //排他思想
  //点击时，所有复选框都不勾选
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  })
  //复选框选中时勾选
  table.value.toggleRowSelection(row, true)
  //图片地址
  skuParams.skuDefaultImg = row.imgUrl
}

//保存按钮的回调
const save = async () => {
  //整理参数，发送请求
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId,
      })
    }
    // console.log(prev);

    return prev
  }, [])

  /*
    //销售属性
    skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev:any,next:any) => {
      if (next.saleIdAndValueId) {
        let [saleAttrId,saleAttrValueId] = next.saleIdAndValueId.split(':')

        prev.push({
          saleAttrId,saleAttrValueId
        })
      }
      return prev
    },[])

    */

  //发送请求,添加sku
  let result = await reqAddSku(skuParams)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功',
    })
    //切换场景为0
    $emit('changeScene', { flag: 0, params: '' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败',
    })
  }
}

//取消按钮的回调
const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' })
}
//对外暴露的方法
defineExpose({ initSkuData })
</script>

<style lang="scss" scoped></style>
